package com.starblink.message.messagesystem.ui

import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.basic.route.RoutePage
import com.starblink.android.basic.base.activity.BaseTVMActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.QuickAdapterHelper
import com.starblink.android.basic.bean.LoadAction
import com.starblink.android.common.view.decoration.SpaceItemDecoration
import com.starblink.basic.log.SKLogger
import com.starblink.basic.style.title.OnTitleBarListener
import com.starblink.basic.style.title.TitleBar
import com.starblink.basic.util.CommUtils
import com.starblink.message.R
import com.starblink.message.adapter.FooterAdapter
import com.starblink.message.databinding.ActivityMessageSystemBinding
import com.starblink.message.messagefollower.ui.MessageFollowerVM
import com.starblink.message.messagelikeorcomment.adapter.MessageSystemAdapter

/**
 * 系统消息页面
 */
@Route(path = RoutePage.Message.MESSAGE_SYSTEM_NOTIFICATION)
class MessageSystemActivity : BaseTVMActivity<ActivityMessageSystemBinding, MessageSystemVM>() {
    private val mAdapter = MessageSystemAdapter()

    override fun initViewBinding(): ActivityMessageSystemBinding =
        ActivityMessageSystemBinding.inflate(layoutInflater)

    override fun initView() {
        super.initView()
        viewBinding.titleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                finish()
            }
        })

        viewBinding.recyclerView.layoutManager = LinearLayoutManager(this)
        viewBinding.recyclerView.addItemDecoration(
            SpaceItemDecoration.newVerticalDecoration(CommUtils.dip2px(8))
        )
        viewBinding.recyclerView.setHasFixedSize(true)
        viewBinding.recyclerView.itemAnimator = null
        mAdapter.setEmptyViewLayout(this, R.layout.layout_empty_view_message)
        val adapterHelper = QuickAdapterHelper.Builder(mAdapter).build()
        viewBinding.recyclerView.adapter = adapterHelper.adapter
//        adapterHelper.addAfterAdapter(FooterAdapter()) // 列表末尾添加一个空白Footer

        viewBinding.smartRefreshLayout.setEnableAutoLoadMore(true)
        viewBinding.smartRefreshLayout.setOnLoadMoreListener {
            viewModel.getSystemMessageList(refresh = false)
        }
    }

    override fun initObservable() {
        super.initObservable()
        viewModel.listResponseLiveData.observe(this)
        {
            if (mAdapter.items.isEmpty() || it.refresh) {
                // 刷新或页面销毁恢复
                mAdapter.submitList(ArrayList(it.totalList))
                if (it.totalList.size < viewModel.curPageNo * MessageSystemVM.PAGE_SIZE) {
                    viewBinding.smartRefreshLayout.setNoMoreData(true)
                    viewBinding.smartRefreshLayout.postDelayed({
                        viewBinding.smartRefreshLayout.setEnableLoadMore(false)
                    },300)
                }
            } else {
                // 加载更多
                mAdapter.addAll(it.pageList)
                if (it.pageList.size < MessageSystemVM.PAGE_SIZE) {
                    viewBinding.smartRefreshLayout.setNoMoreData(true)
                    viewBinding.smartRefreshLayout.postDelayed({
                        viewBinding.smartRefreshLayout.setEnableLoadMore(false)
                    },300)
                }
            }
        }
        viewModel.mLoadActionLiveData.observe(this) {
            when (it.action) {
                LoadAction.STATE_SUCCESS, LoadAction.STATE_ERROR -> {
                    if (it.action == LoadAction.STATE_SUCCESS) {
                        mAdapter.isEmptyViewEnable = true
                    }
                    if (viewBinding.smartRefreshLayout.isLoading) {
                        viewBinding.smartRefreshLayout.finishLoadMore()
                    }
                }
            }
        }
    }

    override fun initLoad() {
        super.initLoad()
        viewModel.getSystemMessageList(true)
    }
}